namespace Project.MVC.PhanQuyen.MsSQLDAL
{
    using System;
    using System.Linq;
    using IDAL;
    using Model;

    public class PQCongViecRepository : IPQCongViec
    {
        LinqPhanQuyenDataContext dataContext;

        public PQCongViecRepository()
        {
            dataContext = new LinqPhanQuyenDataContext();
        }

        #region IPQCongViec Members
        public PQCongViecInfo GetByKey(int id)
        {
            try
            {
                var info = (from record in dataContext.PQ_CongViecs
                            where record.id == id && 1 == 1
                            select new PQCongViecInfo
                         {
                             Id = record.id,
                             NhomCongViecInfo = General.GetInfo(record.PQ_NhomCongViec),
                             MaCongViec = record.maCongViec,
                             TenCongViec = record.tenCongViec,
                             TenController = record.tenController,
                             TenAction = record.tenAction,
                             OnMenu = record.onMenu,
                             DoUuTien = record.doUuTien,
                             VuViec = record.vuViec,
                             GhiChu = record.ghiChu,
                             UserName = record.userName,
                             UserFullName = record.userFullName,
                             TimeInput = record.timeInput,
                             RowVersion = record.rowVersion.ToArray(),
                         }).SingleOrDefault();
                        return info;
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region IDALBase<PQCongViecInfo> Members
        public IQueryable<PQCongViecInfo> GetList()
        {
            try
            {
                var list = from record in dataContext.PQ_CongViecs
                           select new PQCongViecInfo
                         {
                             Id = record.id,
                             NhomCongViecInfo = General.GetInfo(record.PQ_NhomCongViec),
                             IdNhomCongViec = record.idNhomCongViec,
                             MaCongViec = record.maCongViec,
                             TenCongViec = record.tenCongViec,
                             TenController = record.tenController,
                             TenAction = record.tenAction,
                             OnMenu = record.onMenu,
                             DoUuTien = record.doUuTien,
                             VuViec = record.vuViec,
                             GhiChu = record.ghiChu,
                             UserName = record.userName,
                             UserFullName = record.userFullName,
                             TimeInput = record.timeInput,
                             RowVersion = record.rowVersion.ToArray(),
                         };
                return list;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool Insert(PQCongViecInfo value)
        {
            if (value == null)
                return false;
            try
            {
                value.Id = getNewID();
                var info = General.GetInfo(value);

                dataContext.PQ_CongViecs.InsertOnSubmit(info);

                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool Update(PQCongViecInfo value)
        {
            if (value == null)
                return false;
            try
            {
                var info = (from record in dataContext.PQ_CongViecs
                            where record.id == value.Id && 1 == 1
                            select record)
                           .SingleOrDefault();

                if (info != null)
                {
                    info.idNhomCongViec =(int)value.IdNhomCongViec;
                    info.maCongViec = value.MaCongViec;
                    info.tenCongViec = value.TenCongViec;
                    info.tenController = value.TenController;
                    info.tenAction = value.TenAction;
                    info.onMenu = value.OnMenu;
                    info.doUuTien = value.DoUuTien;
                    info.vuViec = value.VuViec;
                    info.ghiChu = value.GhiChu;
                    info.userName = value.UserName;
                    info.userFullName = value.UserFullName;
                    info.timeInput = value.TimeInput;
                    //info.rowVersion = value.RowVersion;
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool Delete(PQCongViecInfo value)
        {
            if (value == null)
                return false;
            try
            {
                var info = (from record in dataContext.PQ_CongViecs
                            where record.id == value.Id && 1 == 1
                            select record)
                           .SingleOrDefault();

                if (info != null)
                {
                    dataContext.PQ_CongViecs.DeleteOnSubmit(info);
                    return true;
                }

                return false;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int Count()
        {
            try
            {
                var value = (from record in dataContext.PQ_CongViecs
                             select record)
                            .Count();
                return value;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public void Commit()
        {
            dataContext.SubmitChanges();
        }
        #endregion

        #region Private Methods
        protected int getNewID()
        {
            try
            {
                int? id = (from record in dataContext.PQ_CongViecs
                           select (int?)record.id)
                          .Max();
                if (id == null)
                    return 1;
                else
                    return ((int)id) + 1;
            }
            catch (Exception)
            {
                return 1;
            }
        }
        #endregion
    }
}
